using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    public partial class VWeaponStrategicWarfareDialog : Form
    {
        private int numTotalV1s;
        private int numTotalV2s;
        private int numFigthers;
        private int flakLevel;
        private int WERPs;
        private int numV1sLaunched = 0;
        private int numV2sLaunched = 0;
        private int numV1sShotDownByFighters = 0;
        private int numV1sShotDownByFlak = 0;
        private int numWERPsShotDownByV1s = 0;
        private int numWERPsShotDownByV2s = 0;
        private Random r;

        public VWeaponStrategicWarfareDialog(int numTotalV1s, int numTotalV2s, int numFigthers, int flakLevel, int WERPs, Random r)
        {
            InitializeComponent();
            this.numTotalV1s = numTotalV1s;
            this.numTotalV2s = numTotalV2s;
            this.numFigthers = numFigthers;
            this.flakLevel = flakLevel;
            this.WERPs = WERPs;
            this.r = r;
            
            closeButton.Enabled = false;
            V1FiredUponByFlakGroupBox.Visible = false;
            V1sFiringGroupBox.Visible = false;
            V1FiredUponByFightersPictureBox.Visible = false;
            numV1FiredUponByFightersLabel.Visible = false;
            fighterHitsPictureBox.Visible = false;
            numFighterHitsLabel.Visible = false;
            V1LeftAfterFighterHitsPictureBox.Visible = false;
            numV1LeftAfterFighterHitsLabel.Visible = false;
            WERPsFiredUponByV2sPictureBox.Visible = false;
            numWERPsFiredUponByV2sLabel.Visible = false;
            V2HitsPictureBox.Visible = false;
            V2AfterFiringPictureBox.Visible = false;
            numV2AfterFiringLabel.Visible = false;
            WERPsLeftAfterV2PictureBox.Visible = false;
            numWERPsLeftAfterV2Label.Visible = false;
            numV2HitsLabel.Visible = false;
            numFighterLabel.Text = numFigthers.ToString();
            numFlakLabel.Text = flakLevel.ToString();
            numWERPsFiredUponByV1sLabel.Text = WERPs.ToString();

            if (numTotalV1s > 0)
            {
                V1sLaunchedhScrollBar.Minimum = 0;
                V1sLaunchedhScrollBar.Maximum = numTotalV1s;
                V1sLaunchedhScrollBar.Value = (int)Math.Ceiling((double)numTotalV1s / 2);
                numV1sLaunched = V1sLaunchedhScrollBar.Value;
                numV1FiredUponByFightersLabel.Text = V1sLaunchedhScrollBar.Value.ToString();
                numV1sNotLaunchedLabel.Text = (numTotalV1s - V1sLaunchedhScrollBar.Value).ToString();
                V1FireUponByFigthersGroupBox.Visible = true;
                V1FiredUponByFightersPictureBox.Visible = true;
                numV1FiredUponByFightersLabel.Visible = true;
            }
            else
                V1FireUponByFigthersGroupBox.Visible = false;

            if (numTotalV2s > 0)
            {
                V2sLaunchedhScrollBar.Minimum = 0;
                V2sLaunchedhScrollBar.Maximum = numTotalV2s;
                V2sLaunchedhScrollBar.Value = (int)Math.Ceiling((double)numTotalV2s / 2);
                numV2sLaunched = V2sLaunchedhScrollBar.Value;
                numV2sLaunchedLabel.Text = V2sLaunchedhScrollBar.Value.ToString();
                numV2sNotLaunchedLabel.Text = (numTotalV2s - V2sLaunchedhScrollBar.Value).ToString();
                V2sFiringGroupBox.Visible = true;
            }
            else
                V2sFiringGroupBox.Visible = false;
        }

        public VWeaponStrategicWarfareDialog(int numTotalV1s, int numTotalV2s, int numFigthers, int flakLevel, int WERPs, 
            int numV1sLaunched, int numV2sLaunched, int NumV1sShotDownByFighters, int NumV1sShotDownByFlak, int numWERPsShotDownByV1s, int numWERPsShotDownByV2s)
        {
            InitializeComponent();
            doVWeaponAttackButton.Visible = false;
            if(numV1sLaunched > 0)
            {
                V1FireUponByFigthersGroupBox.Visible = true;
                V1sLaunchedhScrollBar.Minimum = 0;
                V1sLaunchedhScrollBar.Maximum = numTotalV1s;
                V1sLaunchedhScrollBar.Value = numV1sLaunched;
                V1sLaunchedhScrollBar.Enabled = false;
                numV1FiredUponByFightersLabel.Text = V1sLaunchedhScrollBar.Value.ToString();
                numV1sNotLaunchedLabel.Text = (numTotalV1s - V1sLaunchedhScrollBar.Value).ToString();
                numFighterHitsLabel.Text = NumV1sShotDownByFighters.ToString();
                if ((numV1sLaunched - NumV1sShotDownByFighters) > 0)
                    numV1LeftAfterFighterHitsLabel.Text = (numV1sLaunched - NumV1sShotDownByFighters).ToString();
                else
                    numV1LeftAfterFighterHitsLabel.Text = "0";
                if ((numV1sLaunched - NumV1sShotDownByFighters) > 0)
                {
                    V1FiredUponByFlakGroupBox.Visible = true;
                    numFlakLabel.Text = flakLevel.ToString();
                    if ((numV1sLaunched - NumV1sShotDownByFighters) > 0)
                        numV1FiredUponByFlakLabel.Text = (numV1sLaunched - NumV1sShotDownByFighters).ToString();
                    else
                        numV1FiredUponByFlakLabel.Text = "0";
                    numFlakHitsLabel.Text = NumV1sShotDownByFlak.ToString();
                    if ((numV1sLaunched - NumV1sShotDownByFighters - NumV1sShotDownByFlak) > 0)
                        numV1LeftOverAfterFlakFireLabel.Text = (numV1sLaunched - NumV1sShotDownByFighters - NumV1sShotDownByFlak).ToString();
                    else
                        numV1LeftOverAfterFlakFireLabel.Text = "0";
                    if ((numV1sLaunched - NumV1sShotDownByFighters - NumV1sShotDownByFlak) > 0)
                    {
                        V1sFiringGroupBox.Visible = true;
                        numV1sFiringLabel.Text = (numV1sLaunched - NumV1sShotDownByFighters - NumV1sShotDownByFlak).ToString();
                        numWERPsFiredUponByV1sLabel.Text = WERPs.ToString();
                        numV1HitsLabel.Text = numWERPsShotDownByV1s.ToString();
                        numV1AfterFiringLabel.Text = "0";
                        if ((WERPs - numWERPsShotDownByV1s) > 0)
                            numWERPsLeftAfterV1Label.Text = (WERPs - numWERPsShotDownByV1s).ToString();
                        else
                            numWERPsLeftAfterV1Label.Text = "0";
                    }
                }
            }

            if(numV2sLaunched > 0)
            {
                V2sFiringGroupBox.Visible = true;
                V2sLaunchedhScrollBar.Minimum = 0;
                V2sLaunchedhScrollBar.Maximum = numTotalV2s;
                V2sLaunchedhScrollBar.Value = numV2sLaunched;
                V2sLaunchedhScrollBar.Enabled = false;
                numV2sNotLaunchedLabel.Text = (numTotalV2s - V2sLaunchedhScrollBar.Value).ToString();
                numV2sLaunchedLabel.Text = numV2sLaunched.ToString();
                if ((WERPs - numWERPsShotDownByV1s) > 0)
                    numWERPsFiredUponByV2sLabel.Text = (WERPs - numWERPsShotDownByV1s).ToString();
                else
                    numWERPsFiredUponByV2sLabel.Text = "0";
                numV2HitsLabel.Text = numWERPsShotDownByV2s.ToString();
                numV2AfterFiringLabel.Text = "0";
                if ((WERPs - numWERPsShotDownByV1s - numWERPsShotDownByV2s) > 0)
                    numWERPsLeftAfterV2Label.Text = (WERPs - numWERPsShotDownByV1s - numWERPsShotDownByV2s).ToString();
                else
                    numWERPsLeftAfterV2Label.Text = "0";
            }
        }

        public int V1sLaunched
        {
            get
            {
                return numV1sLaunched;
            }
        }

        public int V2sLaunched
        {
            get
            {
                return numV2sLaunched;
            }
        }

        public int V1sDestroyed
        {
            get
            {
                return numV1sLaunched;
            }
        }

        public int V2sDestroyed
        {
            get
            {
                return numV2sLaunched;
            }
        }

        public int V1WERPHits
        {
            get
            {
                return numWERPsShotDownByV1s;
            }
        }

        public int V2WERPHits
        {
            get
            {
                return numWERPsShotDownByV2s;
            }
        }
        
        public int NumV1sShotDownByFighters
        {
            get
            {
                return numV1sShotDownByFighters;
            }
        }

        public int NumV1sShotDownByFlak
        {
            get
            {
                return numV1sShotDownByFlak;
            }
        }

        private void V1sLaunchedhScrollBar_ValueChanged(object sender, EventArgs e)
        {
            if (V1sLaunchedhScrollBar.Value < (int)Math.Ceiling((double)numTotalV1s / 2))
                V1sLaunchedhScrollBar.Value = (int)Math.Ceiling((double)numTotalV1s / 2);
            numV1FiredUponByFightersLabel.Text = V1sLaunchedhScrollBar.Value.ToString();
            numV1sNotLaunchedLabel.Text = (numTotalV1s - V1sLaunchedhScrollBar.Value).ToString();
            numV1sLaunched = V1sLaunchedhScrollBar.Value;
        }

        private void V2sLaunchedhScrollBar_ValueChanged(object sender, EventArgs e)
        {
            if (V2sLaunchedhScrollBar.Value < (int)Math.Ceiling((double)numTotalV2s / 2))
                V2sLaunchedhScrollBar.Value = (int)Math.Ceiling((double)numTotalV2s / 2);
            numV2sLaunchedLabel.Text = V2sLaunchedhScrollBar.Value.ToString();
            numV2sNotLaunchedLabel.Text = (numTotalV2s - V2sLaunchedhScrollBar.Value).ToString();
            numV2sLaunched = V2sLaunchedhScrollBar.Value;
        }

        private void doVWeaponAttackButton_Click(object sender, EventArgs e)
        {
            if(numTotalV1s > 0)
            {
                fighterPictureBox.Visible = true;
                numFighterLabel.Visible = true;
                numFighterHitsLabel.Visible = true;
                fighterHitsPictureBox.Visible = true;
                numV1LeftAfterFighterHitsLabel.Visible = true;
                V1LeftAfterFighterHitsPictureBox.Visible = true;
                V1sLaunchedhScrollBar.Enabled = false;
                for(int i = 0; i < numFigthers; i++)
                {
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    if (dieRoll >= 5)
                        numV1sShotDownByFighters++;
                    if(numV1sShotDownByFighters >= V1sLaunchedhScrollBar.Value)
                    {
                        numV1sShotDownByFighters = V1sLaunchedhScrollBar.Value;
                        break;
                    }
                }
                numFighterHitsLabel.Text = numV1sShotDownByFighters.ToString();
                numV1LeftAfterFighterHitsLabel.Text = (V1sLaunchedhScrollBar.Value - numV1sShotDownByFighters).ToString();

                if((V1sLaunchedhScrollBar.Value - numV1sShotDownByFighters) > 0)
                {
                    V1FiredUponByFlakGroupBox.Visible = true;
                    numV1FiredUponByFlakLabel.Text = (V1sLaunchedhScrollBar.Value - numV1sShotDownByFighters).ToString();
                    int leftOverV1s = V1sLaunchedhScrollBar.Value - numV1sShotDownByFighters;
                    int numDice = 0;
                    switch (this.flakLevel)
                    {
                        case 0:
                            numDice = 0;
                            break;
                        case 1:
                            numDice = leftOverV1s / 5;
                            break;
                        case 2:
                            numDice = leftOverV1s / 4;
                            break;
                        case 3:
                            numDice = leftOverV1s / 3;
                            break;
                        case 4:
                            numDice = leftOverV1s / 2;
                            break;
                    }

                    for (int i = 0; i < numDice; i++)
                    {
                        uint dieRoll = (uint)(r.Next(6) + 1);
                        if (dieRoll == 6)
                        {
                            this.numV1sShotDownByFlak++;
                        }
                        if (numV1sShotDownByFlak >= leftOverV1s)
                            break;
                    }
                    numFlakHitsLabel.Text = numV1sShotDownByFlak.ToString();
                    numV1LeftOverAfterFlakFireLabel.Text = (leftOverV1s - numV1sShotDownByFlak).ToString();
                    if((leftOverV1s - numV1sShotDownByFlak) > 0)
                    {
                        V1sFiringGroupBox.Visible = true;
                        for (int i = 0; i < (leftOverV1s - numV1sShotDownByFlak); i++)
                        {
                            uint dieRoll = (uint)(r.Next(6) + 1);
                            switch(dieRoll)
                            {
                                case 3:
                                case 4:
                                    this.numWERPsShotDownByV1s++;
                                    break;
                                case 5:
                                case 6:
                                    this.numWERPsShotDownByV1s += 2;
                                    break;
                            }
                        }

                        numV1sFiringLabel.Text = (leftOverV1s - numV1sShotDownByFlak).ToString();
                        numV1HitsLabel.Text = numWERPsShotDownByV1s.ToString();
                        numV1AfterFiringLabel.Text = "0";
                        numWERPsLeftAfterV1Label.Text = (this.WERPs - numWERPsShotDownByV1s).ToString();
                    }
                    else
                    {
                        numV1sFiringLabel.Text = "0";
                        numV1HitsLabel.Text = "0";
                        numV1AfterFiringLabel.Text = (numTotalV1s - V1sLaunchedhScrollBar.Value).ToString();
                        numWERPsLeftAfterV1Label.Text = this.WERPs.ToString();
                    }
                }
                else
                {
                    numV1FiredUponByFlakLabel.Text = "0";
                    numFlakHitsLabel.Text = "0";
                    numV1LeftOverAfterFlakFireLabel.Text = "0";
                    numV1sFiringLabel.Text = "0";
                    numV1HitsLabel.Text = "0";
                    numV1AfterFiringLabel.Text = (numTotalV1s - V1sLaunchedhScrollBar.Value).ToString();
                    numWERPsLeftAfterV1Label.Text = this.WERPs.ToString();
                }
            }

            if(numTotalV2s > 0)
            {
                numWERPsFiredUponByV2sLabel.Visible = true;
                WERPsFiredUponByV2sPictureBox.Visible = true;
                numWERPsFiredUponByV2sLabel.Text = (this.WERPs - numWERPsShotDownByV1s).ToString();
                numV2HitsLabel.Visible = true;
                V2HitsPictureBox.Visible = true;
                numV2AfterFiringLabel.Visible = true;
                V2AfterFiringPictureBox.Visible = true;
                numWERPsLeftAfterV2Label.Visible = true;
                WERPsLeftAfterV2PictureBox.Visible = true;
                numV2AfterFiringLabel.Visible = true;
                numWERPsLeftAfterV2Label.Visible = true;
                numWERPsFiredUponByV2sLabel.Visible = true;
                V2sLaunchedhScrollBar.Enabled = false;

                for (int i = 0; i < numV2sLaunched; i++)
                {
                    uint dieRoll = (uint)(r.Next(6) + 1);
                    switch (dieRoll)
                    {
                        case 2:
                        case 3:
                            this.numWERPsShotDownByV2s++;
                            break;
                        case 4:
                        case 5:
                        case 6:
                            this.numWERPsShotDownByV2s += 2;
                            break;
                    }
                }

                numV2sLaunchedLabel.Text = V2sLaunchedhScrollBar.Value.ToString();
                numV2HitsLabel.Text = numWERPsShotDownByV2s.ToString();
                numV2AfterFiringLabel.Text = "0";
                if ((this.WERPs - numWERPsShotDownByV1s - numWERPsShotDownByV2s) > 0)
                    numWERPsLeftAfterV2Label.Text = (this.WERPs - numWERPsShotDownByV1s - numWERPsShotDownByV2s).ToString();
                else
                    numWERPsLeftAfterV2Label.Text = "0";
                closeButton.Enabled = true;
                doVWeaponAttackButton.Enabled = false;
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void eyeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}